*! version 1.2, 24may2020
*! Peter Hull (hull@uchicago.edu), Raymond Han (rwhan@mit.edu)
program define ivvam_interpret, eclass
	version 16.0
	syntax [anything], [sigma(name)] [gamma(name)] [SECTors(varlist)] [Weights(varlist)]

	matrix b=e(b)
	matrix V=e(V)

	preserve

	if "`anything'" != "" | "`sectors'" != "" {
		keep `anything' `sectors' `weights'
		qui duplicates drop

		//unconditional variance of VA fits (mu*X)
		qui corr `anything' `sectors', cov
		mat Sigma = r(C)
		local plus
		foreach i of varlist `anything' `sectors'{
			foreach j of varlist `anything' `sectors'{
				local var_mu_X `var_mu_X' `plus' [Predictors]_b[`i']*[Predictors]_b[`j']*`=Sigma["`i'","`j'"]'
				local plus +
			}
			//reposting from ivvam
			local mu `mu' (mu_`i': [Predictors]_b[`i'])
		}

		//reposting from ivvam
		local sigma_nu (sigma_nu: [Residual]_b[sigma])

		ereturn post b V
	}

	//delta method SE's
	if ([Residual]_b[sigma]>.0001 & "`anything'" != "") {
		nlcom (sigma_beta_uncond: sqrt((`var_mu_X')+[Residual]_b[sigma]^2)) `mu' `sigma_nu', noheader post
	}
	else if "`sectors'"!="" {
		nlcom (sigma_beta_uncond: sqrt((`var_mu_X'))) `mu' `sigma_nu', noheader post
	}
	else {
		ereturn clear
		di as txt "No estimation performed: negative residual variance estimate and no sectors specified"
	}
	restore

end
